package cn.allinmd.indi.applymodelxian.model.authenticate.bizz.impl;

import android.content.Context;

import java.lang.reflect.Field;
import java.util.List;

import cn.allinmd.indi.applymodelxian.model.authenticate.bizz.IAuthModel;
import cn.allinmd.indi.applymodelxian.model.authenticate.bizz.inner.IUserDao;
import cn.allinmd.indi.applymodelxian.model.authenticate.entity.User;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * @author: XianXueLiang
 * @date: 2016/6/22
 */
public class UserDaoImpl implements IUserDao {

    //Bmob后端云测试必须context
    private Context mContext;

    public UserDaoImpl(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void queryByname(String name, final IAuthModel.OnAuthStateListener<User> listener) {
        BmobQuery<BmobUser> bmobQuery = new BmobQuery<>("_User");
        //BmobQuery<BmobUser> query = bmobQuery.setSQL("select * from _User where username=" + name);
        BmobQuery<BmobUser> query = bmobQuery.addWhereEqualTo("username",name);
        query.setLimit(1);
        query.findObjects(mContext, new FindListener<BmobUser>() {
            @Override
            public void onSuccess(List<BmobUser> list) {
                BmobUser user = list.get(0);
                User u = null;
                try {
                    Class<?> clazz = Class.forName("cn.allinmd.indi.applymodelxian.model.authenticate.entity.User");
                    u = (User) clazz.newInstance();
                    Field[] fields = clazz.getDeclaredFields();
                    for (Field field : fields) {
                        field.setAccessible(true);
                        switch (field.getName()) {
                            case "objectid":
                                field.set(u,user.getObjectId());
                                break;
                            case "username":
                                field.set(u,user.getUsername());
                                break;
                            case "emailVerified":
                                field.setBoolean(u,user.getEmailVerified());
                                break;
                            case "email":
                                field.set(u,user.getEmail());
                                break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    listener.failure(9015,e.getMessage());
                }
                listener.success(u,user.getSessionToken());
            }

            @Override
            public void onError(int i, String s) {
                listener.failure(i,s);
            }
        });
    }

    @Override
    public void queryById(long id,IAuthModel.OnAuthStateListener<User> listener) {
    }

    @Override
    public void insert(User user, IAuthModel.OnAuthStateListener<User> listener) {
        final IAuthModel.OnAuthStateListener<User> mListener = listener;
        if (mListener == null) {
            throw new RuntimeException(listener.getClass().getName()+" must be implemented,not null!!!");
        }
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername(user.getUsername());
        bmobUser.setPassword(user.getPassword());
        bmobUser.setMobilePhoneNumberVerified(user.getMobilePhoneNumberVerified());
        bmobUser.setMobilePhoneNumber(user.getMobilePhoneNumber());
        bmobUser.setEmailVerified(user.getEmailVerified());
        bmobUser.setEmail(user.getEmail());
        bmobUser.signUp(mContext, new SaveListener() {
            @Override
            public void onSuccess() {
                mListener.success(null,null);
            }

            @Override
            public void onFailure(int i, String s) {
                mListener.failure(i,s);
            }
        });

    }

    @Override
    public void deleteById(long id, IAuthModel.OnAuthStateListener<User> listener) {

    }

    @Override
    public void update(User user, IAuthModel.OnAuthStateListener<User> listener) {

    }

    @Override
    public List<User> queryAll() {
        return null;
    }
}
